//过期数据清理，日常，邮件，活动，任务

const mvc = require('cosjs.mvc');
const model = ['daily', 'mail', 'active'];

module.exports = function () {
    let sid = mvc.config.get("sid") || [];
    if (sid.length < 1) {
        return worker_finish();
    }
    let task = mvc.library("multi", sid, worker_begin);
    task.start().then(() => {
        worker_finish();
    }).catch(err => {
        console.log(`worker error`, err);
        worker_finish();
    })

}



//定时器,  string || [schedule,arg...]
module.exports.schedule = [
    ['30 1 5 * * *']
]



function worker_finish() {
    process.exit();
}


function worker_begin(sid) {
    let task = mvc.library("multi", model, function (key) {
        let name = ['worker', 'clear', key].join('-');
        let locks = mvc.model.redis('locks', sid, name);
        locks.lockTime = 60;
        return locks.start(sid).then(ret => {
            if (ret.length == 0) {
                let M = mvc.model.mongo(key, sid);
                if (typeof M.clear === 'function') {
                    return M.clear();
                }
            }
        })
    });
    return task.start()
}
